package cn.admobiletop.adsuyi.adapter.gdt.loader;

import android.app.Activity;
import android.widget.RelativeLayout;

import com.qq.e.ads.banner2.UnifiedBannerView;

import cn.admobiletop.adsuyi.ad.ADSuyiBannerAd;
import cn.admobiletop.adsuyi.ad.adapter.ADSuyiAdapterLoader;
import cn.admobiletop.adsuyi.ad.adapter.ADSuyiAdapterParams;
import cn.admobiletop.adsuyi.ad.data.ADSuyiPlatformPosId;
import cn.admobiletop.adsuyi.ad.listener.ADSuyiBannerAdListener;
import cn.admobiletop.adsuyi.adapter.gdt.listener.BannerAdListener;
import cn.admobiletop.adsuyi.util.ADSuyiAdUtil;
import cn.admobiletop.adsuyi.util.ADSuyiViewUtil;

/**
 * @author ciba
 * @description 横幅（banner）广告加载器
 *              需要实现ADSuyiAdapterLoader<ADSuyiBannerAd, ADSuyiBannerAdListener>
 * @date 2020/4/7
 */
public class BannerAdLoader implements ADSuyiAdapterLoader<ADSuyiBannerAd, ADSuyiBannerAdListener> {
    private BannerAdListener bannerAdListener;
    private UnifiedBannerView unifiedBannerView;

    /**
     * 加载横幅广告
     * @param bannerAd
     * @param adapterParams
     * @param adListener
     */
    @Override
    public void loadAd(ADSuyiBannerAd bannerAd, ADSuyiAdapterParams adapterParams, ADSuyiBannerAdListener adListener) {
        if (!ADSuyiAdUtil.isReleased(bannerAd)
                && bannerAd.getContainer() != null
                && adapterParams != null
                && adapterParams.getPlatform() != null
                && adapterParams.getPlatformPosId() != null
                && adListener != null) {
            ADSuyiPlatformPosId adSuyiPlatformPosId = adapterParams.getPlatformPosId();
            // 广告加载的Activity
            Activity activity = bannerAd.getActivity();
            // 广告布局容器
            RelativeLayout container = bannerAd.getContainer();
            // 广告平台广告位ID
            String platformPosId = adSuyiPlatformPosId.getPlatformPosId();

            loadBannerAd(activity, container, platformPosId, adListener);
        }
    }

    /**
     * 加载广点通横幅广告
     * @param activity        广告展示的Activity
     * @param container       广告布局容器
     * @param platformPosId   广告平台广告位ID
     * @param adListener      广告监听回调
     */
    private void loadBannerAd(Activity activity, RelativeLayout container, String platformPosId, ADSuyiBannerAdListener adListener) {
        // 构造横幅监听类，实现广点通横幅广告回调接口
        bannerAdListener = new BannerAdListener(platformPosId, adListener);

        // 以下是广点通加载广告逻辑
        // 构造广点通UnifiedBannerView，传入必要的参数
        unifiedBannerView = new UnifiedBannerView(activity
                , platformPosId
                , bannerAdListener);
        // 移除开发者传入的banner控件中的布局
        container.removeAllViews();
        // 将广点通unifiedBannerView添加进开发者容器中
        container.addView(unifiedBannerView);
        // 加载并展示广点通开屏广告
        unifiedBannerView.loadAD();
    }

    /**
     * onResumed根据Activity生命周期回调
     */
    @Override
    public void onResumed() {
    }

    /**
     * onPaused根据Activity生命周期回调
     */
    @Override
    public void onPaused() {
    }

    /**
     * ADSuyi广告对象被释放会调用该方法
     */
    @Override
    public void release() {
        if (unifiedBannerView != null) {
            ADSuyiViewUtil.removeSelfFromParent(unifiedBannerView);
            unifiedBannerView.destroy();
            unifiedBannerView = null;
        }

        if (bannerAdListener != null) {
            bannerAdListener.release();
            bannerAdListener = null;
        }
    }
}
